using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class uicontrol_CT_KinhTe : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtNam.Text = DateTime.Now.Year.ToString();
            ResetForm();
            Bind_Data();
        }
    }
    protected void Bind_Data()
    {
        dsKT_Act.SelectParameters.Clear();
        dsKT_Act.SelectParameters.Add("MaSanPham", ddlSP.SelectedValue);
        dsKT_Act.SelectParameters.Add("MaHoiDong", ddlHD.SelectedValue);
        DataTable dt = ((DataView)dsKT_Act.Select(DataSourceSelectArguments.Empty)).Table;
        if (dt.Rows.Count > 0)
        {
            ddlSP.SelectedValue = dt.Rows[0]["MaSanPham"].ToString();
            ddlHD.SelectedValue = dt.Rows[0]["MaHoiDong"].ToString();
            txtTDT.Text = dt.Rows[0]["Q"].ToString();
            txtTSLD.Text = dt.Rows[0]["T"].ToString();
            txtTSCD.Text = dt.Rows[0]["K2"].ToString();
            txtLoiNhuan.Text = dt.Rows[0]["P"].ToString();
            txtGTGT.Text = dt.Rows[0]["Va"].ToString();
            txtNam.Text = dt.Rows[0]["Nam"].ToString();
            
        }
        else 
        {
            txtGTGT.Text = "0";
            txtLoiNhuan.Text = "0";
            txtNam.Text = DateTime.Now.Year.ToString();
            txtTDT.Text = "0";
            txtTSCD.Text = "0";
            txtTSLD.Text = "0";

            btnDongY.Enabled = true;
        }
    }
    
    protected void btnDongY_Click(object sender, EventArgs e)
    {
        try
        {
            dsKT_Act.SelectParameters.Clear();
            dsKT_Act.SelectParameters.Add("MaSanPham", ddlSP.SelectedValue);
            dsKT_Act.SelectParameters.Add("MaHoiDong", ddlHD.SelectedValue);
            DataTable dt = ((DataView) dsKT_Act.Select(DataSourceSelectArguments.Empty)).Table;
            if (dt.Rows.Count > 0)
            {
                dsKT_Act.Update();
            }
            else
            {
                dsKT_Act.Insert();
                btnDongY.Enabled = false;
            }

            dt.Dispose();
            ResetForm();
        }
        catch (Exception ex)
        {
            
            throw ex;
        }
    }
    
    #region Reset()
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ResetForm();
    }
    protected void ResetForm()
    {
        txtGTGT.Text = "0";
        txtLoiNhuan.Text = "0";
        txtNam.Text = DateTime.Now.Year.ToString();
        txtTDT.Text = "0";
        txtTSCD.Text = "0";
        txtTSLD.Text = "0";

        ddlSP.DataBind();
        ddlHD.DataBind();
        ddlHD.SelectedIndex = 0;
        ddlSP.SelectedIndex = 0;
        btnDongY.Enabled = true;
    } 
    #endregion

    protected void ddlHD_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind_Data();
    }
    protected void ddlSP_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind_Data();
    }
}
